import { SAVE_CHANNEL, SAVE_ALL_CHANNELS, SAVE_ARTICLE_LIST } from '@/store/action_types/home'
const initValue = {
    userChannels: [],
    allChannels: [],
    articles: {},
    moreAction: {
        visible: false,
        articleId: '',
        channelId: ''
    }
}

export default function reducer (state=initValue, action) {
    const {type, payload} = action
    switch(type) {
        case SAVE_CHANNEL: 
            return {
                ...state,
                userChannels: payload
            }
        case SAVE_ALL_CHANNELS:
            return {
                ...state,
                allChannels: payload
            }
        case SAVE_ARTICLE_LIST:
            // 如果loadore为true, 代码加载更多数据, 不应该覆盖
            const {list, timestamp, loadMore, channelId} = payload
            // 原来旧的数据
            const oldState = state.articles[channelId]?.list
            return {
                ...state,
                articles: {
                    // 先将原来的articles保存
                    ...state.articles,
                    [channelId]: {
                        timestamp: timestamp,
                        list: loadMore ? [...oldState, ...list] : list
                    }
                }
            }
        case 'home/setMoreAction': 
            return {
                ...state,
                moreAction: payload
            }
        default:
            return state
    }
}